import 'package:flutter/material.dart';
import 'package:jd_flutter/base/foundation/flutter_extension.dart';
import 'package:jd_flutter/base/foundation/jd_colors.dart';
import 'package:jd_flutter/base/router/navigator_util.dart';

typedef TipsTapCallback = void Function(BuildContext context);

class HintDialog extends Dialog {
  final String title;
  final String content;
  final Widget? contentWidget;
  final String leftText;
  final String rightText;
  final Color titleColor;
  final Color contentColor;
  final Color leftTextColor;
  final Color rightTextColor;
  final Color leftButtonColor;
  final Color rightButtonColor;
  final bool cancelable;
  final TipsTapCallback? leftButtonFunction;
  final TipsTapCallback? rightButtonFunction;
  late final bool _showHorizontalDivider;
  late final bool _showVerticalDivider;

  HintDialog({
    super.key,
    this.title = '',
    this.content = '',
    this.contentWidget,
    this.cancelable = true,
    this.titleColor = const Color(0xFF333333),
    this.contentColor = const Color(0xFF333333),
    this.leftText = '取消',
    this.rightText = '确定',
    this.leftTextColor = const Color(0xFF666666),
    this.rightTextColor = const Color(0xFF222222),
    this.leftButtonColor = JDColors.transparent,
    this.rightButtonColor = JDColors.transparent,
    this.leftButtonFunction,
    this.rightButtonFunction,
    bool? showHorizontalDivider,
    bool? showVerticalDivider,
  }) {
    _showHorizontalDivider = showHorizontalDivider ?? true;
    _showVerticalDivider = showHorizontalDivider ??
        (leftButtonFunction != null && rightButtonFunction != null);
  }

  show(BuildContext context) {
    return showDialog(
      context: context,
      barrierDismissible: cancelable,
      useRootNavigator: false,
      builder: (BuildContext context) => this,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: WillPopScope(
        onWillPop: () => Future.value(cancelable),
        child: Center(
          child: Container(
            margin: EdgeInsets.all(24.dp),
            decoration: BoxDecoration(
                color: JDColors.cardColor(context),
                borderRadius: BorderRadius.circular(12.dp)),
            child: ListView(
                shrinkWrap: true,
                physics: const BouncingScrollPhysics(),
                children: <Widget>[
                  SizedBox(height: 16.dp),
                  Visibility(
                    visible: title.isNotEmpty == true,
                    child: Container(
                      constraints: content.isEmpty && contentWidget == null
                          ? BoxConstraints(minHeight: 80.dp)
                          : null,
                      padding: EdgeInsets.fromLTRB(20.dp, 0.dp, 20.dp, 20.dp),
                      alignment: Alignment.center,
                      child: Text(title,
                          style: TextStyle(
                              fontSize: 18.dp,
                              color: titleColor,
                              fontWeight: FontWeight.bold)),
                    ),
                  ),
                  Visibility(
                    visible:
                        content.isNotEmpty == true || contentWidget != null,
                    child: Padding(
                      padding: EdgeInsets.fromLTRB(20.dp, 0.dp, 20.dp, 20.dp),
                      child: contentWidget ??
                          Text(content,
                              style: TextStyle(
                                  fontSize: 15.dp,
                                  color: titleColor,
                                  height: 1.4),
                              textAlign: TextAlign.center),
                    ),
                  ),
                  if (_showHorizontalDivider)
                    Divider(height: .5.dp, thickness: .5.dp),
                  Row(children: [
                    if (leftButtonFunction != null)
                      Expanded(
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () => leftButtonFunction?.call(context),
                          child: Container(
                            height: 46.dp,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: leftButtonColor,
                              borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(8.dp),
                                bottomRight: rightButtonFunction == null
                                    ? Radius.circular(8.dp)
                                    : Radius.zero,
                              ),
                            ),
                            padding: EdgeInsets.only(bottom: 2.dp),
                            child: Text(leftText,
                                style: TextStyle(
                                    fontSize: 15.dp, color: leftTextColor)),
                          ),
                        ),
                      ),
                    if (_showVerticalDivider)
                      SizedBox(
                          height: 46.dp,
                          child:
                              VerticalDivider(width: .5.dp, thickness: .5.dp)),
                    Expanded(
                      child: GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          rightButtonFunction != null
                              ? rightButtonFunction?.call(context)
                              : NavigatorUtil.goBack(context);
                        },
                        child: Container(
                          height: 46.dp,
                          alignment: Alignment.center,
                          padding: EdgeInsets.only(bottom: 2.dp),
                          decoration: BoxDecoration(
                            color: rightButtonColor,
                            borderRadius: BorderRadius.only(
                              bottomLeft: leftButtonFunction == null
                                  ? Radius.circular(8.dp)
                                  : Radius.zero,
                              bottomRight: Radius.circular(8.dp),
                            ),
                          ),
                          child: Text(rightText,
                              style: TextStyle(
                                  fontSize: 15.dp,
                                  color: rightTextColor,
                                  fontWeight: FontWeight.w600)),
                        ),
                      ),
                    ),
                  ]),
                ]),
          ),
        ),
      ),
    );
  }
}
